package com.example.fireassistant_server.controller;

import com.example.fireassistant_server.dto.PointSearchFilter;
import com.example.fireassistant_server.dto.circle.CircleCreateRequest;
import com.example.fireassistant_server.dto.polygon.PolyGonCreateRequest;
import com.example.fireassistant_server.dto.polygon.PolyGonUpdateRequest;
import com.example.fireassistant_server.dto.polyline.PolyLineUpdateRequest;
import com.example.fireassistant_server.entity.PolyGon;
import com.example.fireassistant_server.entity.User;
import com.example.fireassistant_server.mapper.PolyGonMapper;
import com.example.fireassistant_server.service.PolyGonService;
import com.example.fireassistant_server.service.UserService;
import com.example.fireassistant_server.service.WechatUserService;
import com.example.fireassistant_server.vo.CircleVo;
import com.example.fireassistant_server.vo.PolyGonVo;
import com.example.fireassistant_server.vo.PolyLineVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

@RestController
@RequestMapping("/polygon")
public class PolyGonController {

    private WechatUserService wechatUserService;

    private PolyGonMapper polyGonMapper;

    private PolyGonService polyGonService;

    private UserService userService;

    @PostMapping("/add/{user_id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN') ")
    PolyGonVo Add(@Validated @RequestBody PolyGonCreateRequest polyGonCreateRequest, @PathVariable String user_id){
        User user = userService.get(user_id);
        return polyGonMapper.toVo(polyGonService.addGon(polyGonCreateRequest, user));
    }

    // 微信端添加标记线 ；普通管理员可以添加
    @PostMapping("/weChat/add")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN') ")
    PolyGonVo weAdd(@Validated @RequestBody PolyGonCreateRequest polyGonCreateRequest, @RequestHeader(value = "X-WX-OPENID") String openId){
        User user = wechatUserService.getCurrentUser(openId).getUser();
        return polyGonMapper.toVo(polyGonService.addGon(polyGonCreateRequest,user));
    }

    @PostMapping("/search")
    Page<PolyGonVo> search(@Validated @RequestBody PointSearchFilter pointSearchFilter) {
        return polyGonService.search(pointSearchFilter).map(polyGonMapper::toVo);
    }

    //根据id来更新 polyGon
    @PutMapping("/{id}")
    PolyGonVo update(@PathVariable String id,
                      @Validated @RequestBody PolyGonUpdateRequest polyGonUpdateRequest) {
        return polyGonMapper.toVo(polyGonService.update(id,polyGonUpdateRequest));
    }

    //根据id获取区域
    @GetMapping("/{id}")
    PolyGonVo get(@PathVariable String id) {
        return polyGonMapper.toVo(polyGonService.get(id));
    }

    //根据id删除线段
    @DeleteMapping("/{id}")
    @PreAuthorize("hasRole('ROLE_ADMIN') or hasRole('ROLE_SUPER_ADMIN') or hasRole('ROLE_STATION_ADMIN')")
    void delete(@PathVariable String id) {
        polyGonService.delete(id);
    }

    @Autowired
    public void setWechatUserService(WechatUserService wechatUserService) {
        this.wechatUserService = wechatUserService;
    }

    @Autowired
    public void setPolyGonMapper(PolyGonMapper polyGonMapper) {
        this.polyGonMapper = polyGonMapper;
    }

    @Autowired
    public void setPolyGonService(PolyGonService polyGonService) {
        this.polyGonService = polyGonService;
    }

    @Autowired
    public void setUserService(UserService userService) {
        this.userService = userService;
    }
}
